#coding:utf-8
#sushu
import re,sys

#统计n个自然数中，输出素数列表：
signal=1
sushulist=[2]
def sushu(n):
	global signal
	for i in xrange(3,n+1,2):
		for j in xrange(2,int(i/2)+1):
			if i%j==0:
				signal=0
				break
		if(signal==0):
			signal=1
			continue
		sushulist.append(i)
	print "素数输出",sushulist

#输出n个斐波那契数列：
def fab(x):
	fbnqlist=[1,1]
	def fbnq(m):
		if(m==1 or m==2):
			return 1
		else:
			y=int(fbnq(m-1))+int(fbnq(m-2))
			return y

	for i in range(3,x+1):
		fbnqlist.append(fbnq(i))
	print "斐波那契数列",fbnqlist

#反序输出：
def reversed_sequence(ss):
	reversedlist=[]
	l=len(ss)
	for i in xrange(l,0,-1):
		reversedlist.append(ss[i-1])
	if isinstance(ss,str):
		ress=''.join(reversedlist)
		print "字符串反序输出",ress
	if isinstance(ss,tuple):
		ress=tuple(reversedlist)
		print "元祖反序输出",ress	
	if isinstance(ss,list):
		ress=reversedlist
		print "列表反序输出",ress

#转换大小写：
def converted_case(ss):
	re1=r'[a-z-]+'
	re2=r'[A-Z]+'
	tt=[]
	for i in ss:
		t=re.findall(re1,i)
		t1=''.join(t)
		if t1:
			tt.append(t1.upper())
		t=re.findall(re2,i)
		t2=''.join(t)
		if t2:
			tt.append(t2.lower())
	print "转换大小写输出"
	print ''.join(tt)

#判断序列升序，降序，无序：

def which_order(ss):
	seq = []
	for i in xrange(len(ss)):
		if ss[i] is max(ss[i:len(ss)]):
			sig2=1
			seq.append(sig2)
			continue
		if ss[i] is min(ss[i:len(ss)]):
			sig2=0
			seq.append(sig2)
			continue
		else:
			sig2=2
			seq.append(sig2)
	if(sum(seq[:-1])==0):
		print 'up'
	elif(sum(seq[:-1])==len(seq)-1):
		print "down"
	else:
		print "no"

#输入一个字符串，统计出字符串中的元素的个数。
#定义空字典，如果存在就value加1，不存在就新加一个item：

keydict={'whitespase':0, 'other':0, 'a':0, 'b':0, 'c':0, 'd':0, 'e':0, 'f':0, 'g':0, 'h':0, 'i':0, 'j':0, 'k':0, 'l':0, 'm':0, 'n':0, 'o':0, 'p':0, 'q':0, 'r':0, 's':0, 't':0, 'u':0, 'v':0, 'w':0, 'x':0, 'y':0, 'z':0}
def charcount(ss):
	ss=ss.lower()
	for i in ss:
		if i!=' ' and i in re.findall(r'[a-z]',i):
			keydict[i]+=1
		elif i==' ':
			keydict['whitespase']+=1
		else:
			keydict['other']+=1
	print "统计字符个数",keydict

#frange与range类似，一样的参数规则，但是每一项必须要是float类型

def frange(n,step=1,startnum=0):
	ran = []
	i=1
	while True:
		if 1<=i<=n and startnum:
			ran.append(float(i))
			i+=step
		elif 1<=i<=n:
			ran.append(float(i-1))
			i+=step
		else:
			break
	print ran


if __name__ == '__main__':
	# sushu(100)
	# fab(20)
	# reversed_sequence("abcd")
	# reversed_sequence(('a','b','c'))
	# reversed_sequence([2,3,4,5])
	# converted_case("AAc0-88cds#--SS")
	# which_order(ss='abcdefa')
	# which_order(ss='7654321')
	# which_order(ss='7658904321')
	# charcount('Ad d keA   *&^9rrwwd8cd88aaccEDD')
	frange(10,1,1)
	frange(20,3,2)
